from django.shortcuts import render

# Create your views here.
from django.contrib.auth.models import User
from rest_framework import viewsets
from apiv10.serializers import UserSerializer, SupplySerializer, JoinSerializer, CustomizedSerializer, HomeFormSerializer
from django.views.decorators.csrf import csrf_exempt
from apiv10.models import Supply, Join, Customized, HomeForm
from django.http import HttpResponse, JsonResponse
from rest_framework.parsers import JSONParser


class UserViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = User.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer


@csrf_exempt
def Supply_list(request):
    if request.method == 'GET':
        supplys = Supply.objects.all()
        serializer = SupplySerializer(supplys, many=True)
        return JsonResponse(serializer.data, safe=False)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = SupplySerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)


@csrf_exempt
def Supply_detail(request, pk):
    try:
        supply = Supply.objects.get(pk=pk)
    except Supply.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = SupplySerializer(supply)
        return JsonResponse(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = SupplySerializer(supply, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        supply.delete()
        return HttpResponse(status=204)


@csrf_exempt
def Join_list(request):
    if request.method == 'GET':
        joins = Join.objects.all()
        serializer = JoinSerializer(joins, many=True)
        return JsonResponse(serializer.data, safe=False)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = JoinSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)


@csrf_exempt
def Join_detail(request, pk):
    try:
        join = Join.objects.get(pk=pk)
    except Join.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = JoinSerializer(join)
        return JsonResponse(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = JoinSerializer(join, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        join.delete()
        return HttpResponse(status=204)


@csrf_exempt
def Customized_list(request):
    if request.method == 'GET':
        customizeds = Customized.objects.all()
        serializer = CustomizedSerializer(customizeds, many=True)
        return JsonResponse(serializer.data, safe=False)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = CustomizedSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)


@csrf_exempt
def Customized_detail(request, pk):
    try:
        customized = Customized.objects.get(pk=pk)
    except Customized.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = CustomizedSerializer(customized)
        return JsonResponse(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = CustomizedSerializer(customized, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        customized.delete()
        return HttpResponse(status=204)


@csrf_exempt
def HomeForm_list(request):
    if request.method == 'GET':
        HomeForms = HomeForm.objects.all()
        serializer = HomeFormSerializer(HomeForms, many=True)
        return JsonResponse(serializer.data, safe=False)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = HomeFormSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)


@csrf_exempt
def HomeForm_detail(request, pk):
    try:
        homeform = HomeForm.objects.get(pk=pk)
    except HomeForm.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = HomeFormSerializer(homeform)
        return JsonResponse(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = HomeFormSerializer(homeform, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        homeform.delete()
        return HttpResponse(status=204)
